package com.game.api.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.game.api.interceptor.ApiAuthInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author
 *
 */
@Configuration
public class CustomWebMvcConfigurerAdapter extends WebMvcConfigurationSupport {
	@Bean
	public ApiAuthInterceptor apiAuthInterceptor() {
		return new ApiAuthInterceptor();
	}
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(apiAuthInterceptor())
				.addPathPatterns("/**")
				.excludePathPatterns("/pages/**","/error");
	}

	@Override
	protected void addResourceHandlers(ResourceHandlerRegistry registry) {
		// addResourceHandler指的是访问路径，addResourceLocations指的是文件放置的目录
		registry.addResourceHandler("/**").addResourceLocations(
				"classpath:/META-INF/resources/",
				"classpath:/resources/",
				"classpath:/static/",
				"classpath:/public/");
		super.addResourceHandlers(registry);
	}

	@Override
	protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
		super.addArgumentResolvers(argumentResolvers);
//		argumentResolvers.add(new CurrentUserMethodArgumentResolver());
	}

	@Override
	  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	    super.configureMessageConverters(converters);
	    // 1.需要先定义一个convert 转换消息的对象
	    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
	    // 2.添加fastJson的配置信息,比如，是否需要格式化返回的json数据
	    FastJsonConfig fastJsonConfig = new FastJsonConfig();
	    // 空值特别处理
	    // WriteNullListAsEmpty 将Collection类型字段的字段空值输出为[]
	    // WriteNullStringAsEmpty 将字符串类型字段的空值输出为空字符串 ""
	    // WriteNullNumberAsZero 将数值类型字段的空值输出为0
	    // WriteNullBooleanAsFalse 将Boolean类型字段的空值输出为false
	    fastJsonConfig.setSerializerFeatures(
	    		SerializerFeature.PrettyFormat, 
	    		SerializerFeature.WriteNullListAsEmpty,
	    		SerializerFeature.WriteNullStringAsEmpty,
	    		SerializerFeature.WriteDateUseDateFormat
	    		);
	    // 处理中文乱码问题
	    List<MediaType> fastMediaTypes = new ArrayList<>();
	    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
	    fastMediaTypes.add(MediaType.ALL);
	    fastConverter.setSupportedMediaTypes(fastMediaTypes);
	    // 3.在convert中添加配置信息
	    fastConverter.setFastJsonConfig(fastJsonConfig);
	    // 4.将convert添加到converters当中
	    converters.add(fastConverter);

	  }
	

}